package cn.liutao.auth.api;

import cn.hutool.core.util.StrUtil;
import cn.liutao.auth.entity.po.User;
import cn.liutao.auth.service.UserService;
import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.enums.CommonEnum;
import cn.liutao.common.utils.ServletUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author liutao
 * @Description 用户信息管理
 * @date 2023/11/1 8:27
 * @Version 1.0
 */
@WebServlet("/auth/userinfo")
public class UserInfoServlet extends HttpServlet {
    private UserService userService;

    @Override
    public void init() {
        userService = UserService.getInstance();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer userId = LoginUser.getUserId();
        User user = userService.getById(userId);
        if (StrUtil.isBlank(user.getAvatar())) user.setAvatar(CommonEnum.DEFAULT_AVATAR_IMAGE.getValue());
        if (user.getBalance()==null)user.setBalance(BigDecimal.ZERO);
        ServletUtils.ok(resp,user);
    }
}
